package algo_lignes_sites.data.force;


public class ForceAccumulator {

	public enum TypeForce {SUM, MEAN, MEAN_MAX, MAX}
	
	private double xForce;
	private double yForce;
	private double nb_Accu;
	private TypeForce typeForce;
	private double currentTailleForce_sq;

	public ForceAccumulator(TypeForce typeForce) {
		this.typeForce = typeForce;
		clear();
	}

	final public void clear() {
		xForce = 0;
		yForce = 0;
		nb_Accu = 0;
		currentTailleForce_sq = 0;
	}

	public void add(double dx, double dy) {
		if (typeForce==TypeForce.MAX) {
			double tailleToAdd = dx*dx + dy*dy;
			if (tailleToAdd > currentTailleForce_sq) {
				xForce += dx;
				yForce += dy;
				currentTailleForce_sq = tailleToAdd;
			} // sinon on ne fait rien
		} else if (typeForce==TypeForce.SUM) {
			xForce += dx;
			yForce += dy;

		} else if (typeForce==TypeForce.MEAN) {
			// cas des accu type SUM et MEAN
			xForce += dx;
			yForce += dy;
			nb_Accu++;
		} else if (typeForce==TypeForce.MEAN_MAX) {
			double tailleToAdd = dx*dx + dy*dy;
			
			xForce += dx * tailleToAdd;
			yForce += dy * tailleToAdd;
			
			
			nb_Accu += tailleToAdd;
		}
		
	}
	public void add(Force f) {
		add(f.dx, f.dy);
	}

	public Force getForce() {
		if (typeForce==TypeForce.MAX || typeForce==TypeForce.SUM)
			return new Force(xForce, yForce);
		else if (typeForce==TypeForce.MEAN || typeForce==TypeForce.MEAN_MAX) {
			if (nb_Accu==0)
				return new Force(xForce, yForce);
			else
				return new Force(xForce/nb_Accu, yForce/nb_Accu);
		} else {
			assert(false);
			return null;
		}
	}

}
